package com.btw;

import java.util.Date;
import java.util.Random;

import android.util.Log;

public class Ball extends Mesh {
	
	// constants
	private static final float maxFrame = 5;		// Max Frame number
	
	public static final float M_DIAMETER  = 10.0f;
	public static final long FIRING_RATE = 3;
	
	// bitmap's cell's properties
	public static final int M_CELL_WIDTH = 128;
	public static final int M_CELL_HEIGHT = 128;
	
	// fields
//	private float 0;	// X coord
//	private float 0;	// Y coord
	private int health;
	private float frame;		// Frame number
	
	private static final Random r = new Random();
	public long firedTime;
	

	public Ball(float x, float y) {
		this.x = x;
		this.y = y;
		this.frame = 0;
		firedTime = (new Date()).getTime() + r.nextLong() % 1000;		
		
		float[] vertices = new float[] {
				// X, Y, Z;		        
				0 , 			0, 				0.0f,	// top-left
		        0 + M_DIAMETER,	0, 				0.0f,	// top-right
		        0, 				0 + M_DIAMETER, 0.0f,	// bottom-left
		        0 + M_DIAMETER, 0 + M_DIAMETER, 0.0f	// bottom-right
			};
		
		short[] indices = new short[] { 0, 3, 1, 0, 2, 3 };
		
		float[] textureCoordinates = { 
				0.0f, 0.0f, //
				1.0f, 0.0f, //
				0.0f, 1.0f, // 
				1.0f, 1.0f, //
		};
		setVertices(vertices);
		setIndices(indices);
		setTextureCoordinates(textureCoordinates);
	}
	
	public boolean containsPoint(float cX, float cY) {
		boolean goodLeft = x - VSActivity.CLICK_TOLERANCE < cX;
		boolean goodRight = cX < (x + M_DIAMETER + VSActivity.CLICK_TOLERANCE);
		boolean goodTop = y - VSActivity.CLICK_TOLERANCE < cY; 
		boolean goodBottom = cY < (y + M_DIAMETER + VSActivity.CLICK_TOLERANCE); 
		return (goodLeft && goodRight && goodTop && goodBottom);
	}
	
	/**
	 * given coordinates of a mouse click, returns the distance
	 * from the center of the object to the click point. 
	 * @param clickX
	 * @param clickY
	 * @return
	 */
	public float getAccuracy(float clickX, float clickY) {
		float centerX = x + (M_DIAMETER/2);
		float centerY = y + (M_DIAMETER/2);
		
		float dx = clickX - centerX;
		float dy = clickY - centerY;
		
		float h = ((float) Math.hypot(dx, dy));
		Log.d("accuracy", "monster:"+h);
		return h;
	}
	
	@Override
	public String toString() {
		return "Monster->origin:("+x+","+y+") dimensions:(w-"+M_DIAMETER+",h-"+M_DIAMETER+")";
	}
	
	/**
	 * @return health of the monster
	 */
	public int getHealth() {
		return health;
	}
	
	/**
	 * @param amount of health to damage
	 * @return true if the monster has been killed
	 */
	public boolean damageHealth(int amount) {
		this.health -= amount;
		return this.health <= 0;
	}
}